package com.lg.erp.controller.zhangkai;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.SCompany;
import com.lg.erp.entity.SUser;
import com.lg.erp.entity.zhangkai.Relation;
import com.lg.erp.service.ISCompanyService;
import com.lg.erp.service.IUserService;
import com.lg.erp.service.zhangkai.ICustomerService;
import com.lg.erp.service.zhangkai.IRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class RelationController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IRelationService relationService;
    @Autowired
    private ISCompanyService companyService;

    private static SCompany sCompany;
    //分页查询所有的客户
    @RequestMapping(value = "/relation",method = RequestMethod.GET)
    public JsonResult findPage(@RequestParam(value = "curr",required = false,defaultValue = "1")Long curr,@RequestParam(value = "size",required = false,defaultValue = "7")Integer size){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof User) {
            User user = (User) principal;
            SUser one = userService.getOne(new QueryWrapper<SUser>().lambda().eq(SUser::getUsername, user.getUsername()));
            sCompany=companyService.getById(one.getCompanyId());
            Page<Relation> relationPage = new Page<>(curr, size);
            IPage<Relation> page = relationService.page(relationPage,new QueryWrapper<Relation>().lambda().eq(Relation::getSupplierId,one.getCompanyId()));
            page.getRecords().forEach(relation -> {
                relation.setCompany(companyService.getById(relation.getCustomerId()));
            });
            return new JsonResult(1, "ok", page);
        }
        return new JsonResult(0,"ok",null);
    }
    //查询无关公司
    @RequestMapping(value = "/relation/choose",method = RequestMethod.GET)
    public JsonResult findChoose(@RequestParam(value = "curr",required = false,defaultValue = "1")Long curr,@RequestParam(value = "size",required = false,defaultValue = "7")Integer size){
        Page<SCompany> sCompanyPage = new Page<>(curr, size);
        //查询所有无关的公司
        IPage<SCompany> choose = relationService.findChoose(sCompanyPage, sCompany.getId());
       /* List<SCompany> records = choose.getRecords();
        //遍历所有无关的公司
        for (SCompany record : records) {
            List<String> productName = new ArrayList<>();
            productName = relationService.findProductName(record.getId());
            StringBuilder stringBuilder = new StringBuilder();
            for (String s : productName) {
                stringBuilder.append(s+",");
            }
            if (stringBuilder.length()!=0){
                stringBuilder.replace(0,stringBuilder.length()-1,",");
                record.setProductName(String.valueOf(stringBuilder));
            }
        }*/
        return new JsonResult(1,"ok",choose);
    }

    //新增关系
    @RequestMapping(value = "relation",method = RequestMethod.POST)
    public JsonResult addRelation(@RequestBody Relation relation){
        System.out.println(relation);
        if (relation.getCustomerId()==null){
            relation.setCustomerId(sCompany.getId());
        }else {
            relation.setSupplierId(sCompany.getId());
        }
        relationService.save(relation);
        return new JsonResult(1,"ok",null);
    }
    //模糊查询
    @RequestMapping("/relation/{keyWord}/{province}/{city}/{area}/{current}")
    public JsonResult findCompanyByKeyWordAndPage(@PathVariable String keyWord
            ,@PathVariable String province
            ,@PathVariable String city
            ,@PathVariable String area
            ,@PathVariable Integer current) {
        Page<SCompany> page = new Page<>(current, 5);
        IPage<SCompany> pages=companyService.getCompanyByCompanyNameAndPage((keyWord.equals(" ")?"":keyWord)
                ,(province.equals(" ")?"":province),(city.equals(" ")?"":city),(area.equals(" ")?"":area),page);
        List<SCompany> records = pages.getRecords();
        List<Relation> relations = new ArrayList<>();
        records.forEach(sCompany1 -> {
            Relation one = relationService.getOne(new QueryWrapper<Relation>().lambda().eq(Relation::getCustomerId, sCompany1.getId()).eq(Relation::getSupplierId, sCompany.getId()));
            one.setCompany(sCompany1);
            relations.add(one);
        });
        return new JsonResult(1,"ok",relations);
    }

    //模糊查询
    @RequestMapping(value = "/relation/conditionQuery/{companyName}/{productName}",method = RequestMethod.GET)
    public JsonResult findQuery(@PathVariable("companyName")String companyName,@PathVariable("productName")String productName){
        List<SCompany> query = relationService.findQuery(companyName, productName ,sCompany.getId());
        return new JsonResult(1,"ok",query);
    }
}
